package com.yellowcong.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

/**
 * 创建日期:2018年1月11日<br/>
 * 创建时间:下午8:33:36<br/>
 * 创建者 :yellowcong<br/>
 * 机能概要:
 */
public class ReadThread implements Runnable {
	private InputStream in;
	private PrintStream out;
	//编码
	private String charset = "UTF-8";
	
	//用于暂停的flag
	private boolean flag = true;

	// 停止线程
	public void stopThread() {
		flag = false;
	}

	/**
	 * 
	 * @param in  输入流，获取的输入
	 * @param out 输出流
	 */
	public ReadThread(InputStream in, PrintStream out) {
		super();
		this.in = in;
		this.out = out;
	}

	public void run() {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(in, charset));

			String temp;
			
			//读取数据
			while ((temp = br.readLine()) != null && flag == true) {
				if (out != null) {
					out.println(temp);
					out.flush();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
